/*
 * @Author: June
 * @Date: 2022-11-18 22:13:53
 * @LastEditors: 无处安放的六月
 * @LastEditTime: 2023-12-04 21:31:15
 * @Description:
 *
 */
import store from '@/store'
import request from './request'
export async function getFieldsByRoute(route) {
  const res = await request({
    method: 'get',
    url: `/platform/field/list?route=${route}`
  })
  if (res.data.data.length) {
    console.info(route + '：', res.data.data)
  }
  store.dispatch('user/setField', res.data.data)
}

/**
 * @description:  转树结构
 * @param {*} menuList
 * @param {*} id
 * @param {*} parentId
 * @param {*} children
 * @return {*}
 */
export function treeData(menuList, id, parentId, children) {
  const list = JSON.parse(JSON.stringify(menuList))
  return list.filter((father) => {
    // eslint-disable-next-line
    const childList = list.filter((child) => father[id] == child[parentId])
    childList.length > 0 ? (father[children] = childList) : ''
    //  最上级父元素id为0  根据顶级父元素id设置
    // eslint-disable-next-line
    return Number(father[parentId]) == 0 || null || ''
  })
}
/**  树结构数据转扁平化数据 */
const getNodeMap = (node, parentNode) => {
  node.parentNode = parentNode
  const nodeMap = [node]
  if (node.children && node.children.length) {
    node.children.forEach((item) => nodeMap.push(...getNodeMap(item, node)))
  }
  return nodeMap
}

export const getTreeMap = (tree) => {
  if (!(tree instanceof Array)) return
  const treeMap = []
  tree.forEach((node) => {
    treeMap.push(...getNodeMap(node, tree))
  })
  return treeMap
}
/* 时间戳对比 */
export function computationTime(time1, time2) {
  if (time1 && time2) {
    var date1 = new Date(time1)
    var date2 = new Date(time2)
    var s1 = date1.getTime()
    var s2 = date2.getTime()
    var total = (s2 - s1) / 1000
    return total / 60
  } else {
    return 1
  }
}

/**
 * @description: 时间转为 2022-11-30 23:02:30 格式
 * @param {*} time
 * @return {*}
 */
export function timeFormatToString(time) {
  var dateee = new Date(time).toJSON()
  var date = new Date(+new Date(dateee) + 8 * 3600 * 1000)
    .toISOString()
    .replace(/T/g, ' ')
    .replace(/\.[\d]{3}Z/, '')
  return date
}
export function getCurrentDate() {
  var myDate = new Date()
  var year = myDate.getFullYear() // 年
  var month = myDate.getMonth() + 1 // 月
  var day = myDate.getDate() // 日
  const hh = new Date().getHours()
  const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
  const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
  var days = myDate.getDay()
  switch (days) {
    case 1:
      days = '星期一'
      break
    case 2:
      days = '星期二'
      break
    case 3:
      days = '星期三'
      break
    case 4:
      days = '星期四'
      break
    case 5:
      days = '星期五'
      break
    case 6:
      days = '星期六'
      break
    case 0:
      days = '星期日'
      break
  }
  var str = `${hh}:${mf}:${ss}`
  var str2 = `${year}年${month}月${day.toString().length > 1 ? day : '0' + day}日 \xa0${days}`
  return [str, str2]
}

export function convertData(data) {
    var result = [];
    data.forEach(function(item) {
        var newItem = {
            name: item.wmsDetailName,
            value: item.value,
            status:item.status,
            itemStyle: {
              color: item.status === 1 ? '#1eb6fe' : item.status === 2 ? '#04cf48':'#cf3d04'
            }
        };
        if (item.children) {
            newItem.children = convertData(item.children);
        }
        result.push(newItem);
    });
    return result;
}